﻿using System;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace Utils.Thumbnail
{
    class Thumbnail
    {
        static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"E:\IA\Pictures\testPic");
            files = files.Where(a => !a.StartsWith("tmp_")).ToArray();
            if (files.Length < 1) return;

            string imgFile = files.First();
            string putFile = Path.GetDirectoryName(imgFile) + "\\tmp_" + Path.GetFileName(imgFile);
            GetPicThumbnail(imgFile, putFile, 99);
        }

        /// 无损压缩图片  
        /// <param name="sFile">原图片</param>  
        /// <param name="dFile">压缩后保存位置</param>  
        /// <param name="dWidth"></param>  
        /// <param name="dHeight">高度</param>  
        /// <param name="flag">压缩质量(数字越小压缩率越高) 1-100</param>  
        /// <returns></returns>  

        public static bool GetPicThumbnail(string sFile, string dFile, int flag)
        {
            Image iSource = Image.FromFile(sFile);
            ImageFormat tFormat = iSource.RawFormat;
            int sW = iSource.Width > 1366 ? 1366 : iSource.Width,
                sH = iSource.Height > 768 ? 768 : iSource.Height;

            //按比例缩放
            if (iSource.Height > sH || iSource.Width > sW)
            {
                if (iSource.Width * sH > iSource.Width * sW)
                {
                    sH = sW * iSource.Height / iSource.Width;
                }
                else
                {
                    sW = iSource.Width * sH / iSource.Height;
                }
            }

            Bitmap ob = new Bitmap(sW, sH);
            Graphics g = Graphics.FromImage(ob);

            g.Clear(Color.WhiteSmoke);
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //(dWidth - sW) / 2, (dHeight - sH) / 2, sW, sH)
            Rectangle rectangle = new Rectangle(-1, -1, sW + 2, sH + 2);
            g.DrawImage(iSource, rectangle, 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);

            g.Dispose();
            //以下代码为保存图片时，设置压缩质量  
            EncoderParameters ep = new EncoderParameters
            {
                Param = new EncoderParameter[]
                {
                    new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, new long[] { flag })
                }
            };
            try
            {
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;

                jpegICIinfo = arrayICI.Where(a => a.FormatDescription.Equals("JPEG")).FirstOrDefault();

                if (jpegICIinfo != null)
                {
                    ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径  
                }
                else
                {
                    ob.Save(dFile, tFormat);
                }
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
            finally
            {
                iSource.Dispose();
                ob.Dispose();
            }
        }
    }
}
